<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <h2>Promise 初体验</h2>
        <button id="btn">点击抽奖</button>
    </div>
    <script>
        function rand(m, n) {
            return Math.ceil(Math.random() * (n - m + 1)) + m - 1
        }
        const btn = document.getElementById('btn')
        btn.addEventListener('click', () => {
            // console.log('已经点击按钮')
            // setTimeout(() => {
            //     let n = rand(1, 100)
            //     if (n <= 30) {
            //         alert('恭喜中奖了 ')
            //     } else {
            //         alert('再接再厉')
            //     }
            // }, 1000);
            // 用promise来实现
            const p = new Promise((resolve, reject) => {
                setTimeout(() => {
                    let n = rand(1, 100)
                    if (n <= 30) {
                        resolve('中奖了')
                    } else {
                        reject('再接再厉')
                    }
                });
            })
            p.then((res) => {
                console.log(res)
            }, (err) => {
                console.log(err)
            })
        })
    </script>
</body>

</html>